package com.gaojun;

import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.filechooser.FileSystemView;

import org.omg.CORBA.PRIVATE_MEMBER;

import com.gaojun.tool.Constant;
import com.gaojun.tool.PropertiesUtil;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import sun.audio.AudioStream;
import sun.security.krb5.internal.PAEncTSEnc;

/**
 * 录课
 * 
 * @author gj
 *
 */
public class VideoScreenRecording extends Stage {

	private static final int WIDTH = 500;
	private static final int HEIGHT = 60;

	private String DEFAULT_FILE_PATH = ""; // 默认存放路径

	private Label lblTime = null;
	private VideoRecord video = null; // 录屏
	// 计数线程
	private CountingThread thread = null;
	// 记录程序开始时间
	private long programStart = 0;
	// 程序一开始就是暂停的
	private long pauseStart = 0;
	// 程序暂停的总时间
	private long pauseCount = 0;
	private String currentFilePath = ""; // 当前路径
	private double xOffset = 0;
	private double yOffset = 0;
	private boolean isHaveDevice = false; // 是否有录音设备
	private boolean isLive = false;
	private LocalScreentRecordAndPush localScreentRecordAndPush = null;

	public VideoScreenRecording() {
		String style = "-fx-background-color:#00000000";

		/******* 分割线：设置弹出窗的一些样式 ***********/
		this.initStyle(StageStyle.TRANSPARENT); // 设置弹出窗为透明
		this.setWidth(WIDTH);
		this.setHeight(260);
		Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
		this.setX((primaryScreenBounds.getWidth() - WIDTH) / 2);
		this.setY(primaryScreenBounds.getHeight() - 260);
		BorderPane borderPane = new BorderPane();
		borderPane.setStyle(style);
		borderPane.setPrefSize(500, 260);

		// this.getIcons().add(new Image(Constant.LOGO));
		/******* 分割线：设置弹出窗的一些样式 ***********/

		Button add = new Button("");// 添加水印
		Button start = new Button(""); // 开始按钮
		Button play = new Button(""); // 播放按钮
		Button hidden = new Button(""); // 关闭弹窗
		Button live = new Button("");
		Button stop = new Button("");
		Button set = new Button("");
		Button home = new Button("");

		/******* 分割线：设置开始录课按钮的一些样式 ********/
		add.setTooltip(new Tooltip("添加水印"));
		add.setGraphic(new ImageView(new Image("res/images/record/add.png")));
		add.setLayoutX(6);
		add.setLayoutY(14);
		add.setStyle(style);

		live.setTooltip(new Tooltip("直播"));
		live.setGraphic(new ImageView(new Image("res/images/record/live.png")));
		live.setLayoutX(58);
		live.setLayoutY(11);
		live.setStyle(style);
		/******* 分割线：设置开始录课按钮的一些样式 ********/
		start.setTooltip(new Tooltip("录屏"));
		start.setGraphic(new ImageView(new Image("res/images/record/record.png")));
		start.setLayoutX(118);
		start.setLayoutY(14);
		start.setStyle(style);
		/******* 分割线：设置开始录课按钮的一些样式 ********/
		lblTime = new Label("00:00:00");
		lblTime.setStyle("-fx-background-color:#00000000");
		lblTime.setFont(Font.font("KaiTi", FontWeight.BOLD, 16));
		lblTime.setTextFill(Color.WHITE);
		lblTime.setLayoutX(164);
		lblTime.setLayoutY(23);
		lblTime.setPrefSize(80, 25);

		stop.setTooltip(new Tooltip("停止"));
		stop.setGraphic(new ImageView(new Image("res/images/record/stop.png")));
		stop.setLayoutX(243);
		stop.setLayoutY(14);
		stop.setStyle(style);

		/******** 分割线：设置播放按钮的一些样式 *********/
		play.setTooltip(new Tooltip("播放"));
		play.setGraphic(new ImageView(new Image("res/images/record/play.png")));
		play.setLayoutX(295);
		play.setLayoutY(14);
		play.setStyle(style);
		/******** 分割线：设置播放按钮的一些样式 *********/
		set.setTooltip(new Tooltip("设置"));
		set.setGraphic(new ImageView(new Image("res/images/record/set.png")));
		set.setLayoutX(348);
		set.setLayoutY(14);
		set.setStyle(style);

		home.setTooltip(new Tooltip("返回主页"));
		home.setGraphic(new ImageView(new Image("res/images/record/home.png")));
		home.setLayoutX(400);
		home.setLayoutY(14);
		home.setStyle(style);

		/********** 分割线：设置关闭按钮的一些样式 ********/
		hidden.setTooltip(new Tooltip("隐藏控制栏"));
		hidden.setGraphic(new ImageView(new Image("res/images/record/hidden.png")));
		hidden.setLayoutX(450);
		hidden.setLayoutY(14);
		hidden.setStyle(style);
		/********** 分割线：设置关闭按钮的一些样式 ********/

		// 开始录课

		start.setOnAction(e -> {
			if (start.getTooltip().getText().equals("录屏")) {
				try {
					if (video == null) {

						DEFAULT_FILE_PATH = Constant.SAVE_PATH;
						isHaveDevice = isHaveDevice();
						thread = new CountingThread();
						thread.start();
						programStart = System.currentTimeMillis();
						pauseStart = programStart;
						File directory = new File(DEFAULT_FILE_PATH);
						if (!(directory.exists()) && !directory.isDirectory()) {
							directory.mkdirs();
						}
						SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
						currentFilePath = DEFAULT_FILE_PATH + File.separatorChar + df.format(new Date());
						pauseCount += (System.currentTimeMillis() - pauseStart);
						video = new VideoRecord(currentFilePath, isHaveDevice);
					}
					video.start();
					start.setGraphic(new ImageView(new Image("res/images/record/puash.png")));
					start.setTooltip(new Tooltip("暂停"));
					pauseCount += (System.currentTimeMillis() - pauseStart);
					thread.stopped = false;
				} catch (Exception ex) {
					// TODO: handle exception
					thread.stopped = true;
					this.show();
				}

			} else {
				try {
					start.setGraphic(new ImageView(new Image("res/images/record/record.png")));
					start.setTooltip(new Tooltip("录屏"));
					pauseStart = System.currentTimeMillis();
					thread.stopped = true;
					video.pause();
				} catch (org.bytedeco.javacv.FrameRecorder.Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});

		// 调用播放器播放视频
		play.setOnAction(e -> {
			try {
				Desktop.getDesktop().open(new File(currentFilePath + ".mp4"));
				// System.gc();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		});
		// 停止
		stop.setOnAction(e -> {
			if (video != null) {
				start.setGraphic(new ImageView(new Image("res/images/record/record.png")));
				start.setTooltip(new Tooltip("录屏"));
				lblTime.setText("00:00:00");
				programStart = 0;
				pauseStart = 0;
				pauseCount = 0;
				video.stop();
				thread.stopped = true;
				thread = null;
				video = null;
			}

		});

		set.setOnAction(e -> {
			showSetting(borderPane);
		});

		live.setOnAction(e -> {
			if (!isLive) {
				isLive = true;
				localScreentRecordAndPush = new LocalScreentRecordAndPush();
				if (Constant.LIVE_PATH.equals("")) {
					showSetting(borderPane);
				} else {
					localScreentRecordAndPush.screenRecord();
				}
				live.setGraphic(new ImageView(new Image("res/images/record/stoplive.png")));
				live.setTooltip(new Tooltip("停止"));

			} else {
				localScreentRecordAndPush.stop();
				live.setGraphic(new ImageView(new Image("res/images/record/live.png")));
				live.setTooltip(new Tooltip("直播"));
				isLive = false;
			}

		});
		add.setOnAction(e -> {
			addWaterMark(borderPane);
		});
		// 关闭
		hidden.setOnAction(e -> {
			this.setIconified(true);
		});

		home.setOnAction(e -> {
			if (localScreentRecordAndPush != null) {
				localScreentRecordAndPush.stop();
				localScreentRecordAndPush = null;
			}
			if (video != null) {
				video.stop();
				video = null;
				thread.stopped = true;
				thread = null;
			}
			MultimediaProcessingMain main = new MultimediaProcessingMain();
			try {
				main.start(new Stage());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			this.close();
		});

		Pane paneButton = new Pane();
		paneButton.setPrefSize(WIDTH, HEIGHT);
		paneButton.setStyle("-fx-background-color:#000;");
		paneButton.getChildren().addAll(add, live, start, lblTime, stop, play, set, home, hidden);

		// 窗口拖动
		this.addEventHandler(MouseEvent.ANY, new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				event.consume();
				if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
					xOffset = event.getSceneX();
					yOffset = event.getSceneY();
				} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
					VideoScreenRecording.this.setX(event.getScreenX() - xOffset);
					if (event.getScreenY() - yOffset < 0) {
						VideoScreenRecording.this.setY(0);
					} else {
						VideoScreenRecording.this.setY(event.getScreenY() - yOffset);
					}
				}
			}
		});

		borderPane.setBottom(paneButton);
		Scene scene = new Scene(borderPane);
		scene.setFill(null);
		this.getIcons().add(new Image("res/image/stage/logo.png"));
		this.setScene(scene);
	}

	private void addWaterMark(BorderPane borderPane) {
		// TODO Auto-generated method stub
		if (borderPane.getCenter() != null) {
			borderPane.setCenter(null);
			return;
		}
		Pane pane = new Pane();
		pane.setStyle("-fx-background-color:#00000000");
		pane.setPrefSize(500, 200);
		Pane paneButton = new Pane();
		paneButton.setPrefSize(80, 100);
		paneButton.setStyle("-fx-background-color:#000");
		Button btnText = new Button("文 字");
		btnText.setStyle("-fx-background-color:#00000000");
		btnText.setFont(Font.font(18));
		btnText.setTextFill(Color.WHITE);
		btnText.setPrefSize(80, 50);
		btnText.setLayoutX(0);
		btnText.setLayoutY(0);
		btnText.setOnAction(e -> {
			showTextMarkSet(borderPane);
		});

		Button btnimage = new Button("图 片");
		btnimage.setStyle("-fx-background-color:#00000000;-fx-border-color:#fff;-fx-border-width:1 0 0 0");
		btnimage.setFont(Font.font(20));
		btnimage.setTextFill(Color.WHITE);
		btnimage.setPrefSize(80, 50);
		btnimage.setLayoutY(50);
		btnimage.setOnAction(e -> {
			showImageSet(borderPane);
		});
		paneButton.getChildren().addAll(btnText, btnimage);
		paneButton.setLayoutY(100);
		pane.getChildren().add(paneButton);
		borderPane.setCenter(pane);
	}

	private void showImageSet(BorderPane borderPane) {
		// TODO Auto-generated method stub
		Pane pane = new Pane();
		pane.setPrefSize(500, 200);
		pane.setStyle("-fx-background-color:#000");
		TextField txtImgPath = new TextField();
		txtImgPath.setFont(Font.font(16));
		txtImgPath.setPrefSize(320, 35);
		txtImgPath.setLayoutX(40);
		txtImgPath.setLayoutY(65);
		Button btnSet = new Button("选择");
		btnSet.setFont(Font.font(16));
		btnSet.setTextFill(Color.WHITE);
		btnSet.setStyle("-fx-background-color:#808a87");
		btnSet.setLayoutX(380);
		btnSet.setLayoutY(65);
		btnSet.setOnAction(e -> {
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle("选择图片");
			fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Images", "*.*"),
					new FileChooser.ExtensionFilter("JPG", "*.jpg"), new FileChooser.ExtensionFilter("PNG", "*.png"));
			File file = fileChooser.showOpenDialog(this);
			if (file != null) {
				txtImgPath.setText(file.getPath().toString());
			}

		});

		Button btnConfirm = new Button("确定");
		btnConfirm.setFont(Font.font(16));
		btnConfirm.setTextFill(Color.WHITE);
		btnConfirm.setStyle("-fx-background-color:#808a87");
		btnConfirm.setLayoutX(320);
		btnConfirm.setLayoutY(178);
		btnConfirm.setOnAction(e -> {
			Constant.WATER_MARK = txtImgPath.getText().toString();
			Constant.WATER_TYPE = 1;
			borderPane.setCenter(null);
		});

		Button btnCancle = new Button("取消");
		btnCancle.setFont(Font.font(16));
		btnCancle.setTextFill(Color.WHITE);
		btnCancle.setStyle("-fx-background-color:#808a87");
		btnCancle.setLayoutX(400);
		btnCancle.setLayoutY(178);
		btnCancle.setOnAction(e -> {
			borderPane.setCenter(null);
		});

		pane.getChildren().addAll(txtImgPath, btnSet, btnConfirm, btnCancle);
		borderPane.setCenter(pane);
	}

	/**
	 * 设置文字水印
	 * 
	 * @param borderPane
	 */
	private void showTextMarkSet(BorderPane borderPane) {
		// TODO Auto-generated method stub
		Pane pane = new Pane();
		pane.setPrefSize(500, 200);
		pane.setStyle("-fx-background-color:#000");

		TextArea textArea = new TextArea();
		textArea.setStyle("-fx-background-color:#000;-fx-border-width:1px;-fx-border-color:#fff");
		textArea.setPrefSize(460, 60);
		textArea.setLayoutX(20);
		textArea.setLayoutY(15);
		textArea.setFont(Font.font(24));
		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();// GraphicsEnvironment是一个抽象类，不能实例化，只能用其中的静态方法获取一个实例
		String[] str = environment.getAvailableFontFamilyNames();// 获取系统字体
		ObservableList cursors = FXCollections.observableArrayList();
		for (int i = 0; i < str.length; i++) {
			cursors.add(str[i]);
		}
		Label lblFamily = new Label("字体：");
		lblFamily.setTextFill(Color.WHITE);
		lblFamily.setFont(Font.font(16));
		lblFamily.setLayoutX(15);
		lblFamily.setLayoutY(110);
		ComboBox<String> cBox = new ComboBox<>();
		cBox.setItems(cursors);
		cBox.setEditable(true);
		cBox.setPrefSize(200, 30);
		cBox.setLayoutX(55);
		cBox.setLayoutY(105);
		cBox.setValue(Constant.FONT_FAMILY);

		Label lblSize = new Label("大小：");
		lblSize.setTextFill(Color.WHITE);
		lblSize.setFont(Font.font(16));
		lblSize.setLayoutX(265);
		lblSize.setLayoutY(110);
		Spinner sp = new Spinner(24, 500, Constant.FONT_SIZE);
		// sp.setValueFactory(svf);// 设置Value Factory
		sp.setEditable(true);
		sp.setPrefSize(70, 30);
		sp.setLayoutX(307);
		sp.setLayoutY(105);

		Label lblColor = new Label("颜色：");
		lblColor.setTextFill(Color.WHITE);
		lblColor.setFont(Font.font(16));
		lblColor.setLayoutX(387);
		lblColor.setLayoutY(110);
		ColorPicker colorpicker = new ColorPicker();
		colorpicker.setValue(Constant.color);
		colorpicker.setPrefSize(60, 30);
		colorpicker.setLayoutX(425);
		colorpicker.setLayoutY(105);

		Button btnConfirm = new Button("确定");
		btnConfirm.setFont(Font.font(16));
		btnConfirm.setTextFill(Color.WHITE);
		btnConfirm.setStyle("-fx-background-color:#808a87");
		btnConfirm.setLayoutX(320);
		btnConfirm.setLayoutY(178);

		btnConfirm.setOnAction(e -> {
			Constant.FONT_FAMILY = cBox.getValue().toString();
			Constant.FONT_SIZE = (double) sp.getValue();
			Constant.color = colorpicker.getValue();
			Constant.WATER_MARK = textArea.getText().toString();
			Constant.WATER_TYPE = 0;
			borderPane.setCenter(null);
		});

		Button btnCancle = new Button("取消");
		btnCancle.setFont(Font.font(16));
		btnCancle.setTextFill(Color.WHITE);
		btnCancle.setStyle("-fx-background-color:#808a87");
		btnCancle.setLayoutX(400);
		btnCancle.setLayoutY(178);
		btnCancle.setOnAction(e -> {
			borderPane.setCenter(null);
		});

		pane.getChildren().addAll(textArea, cBox, sp, lblFamily, lblSize, lblColor, colorpicker, btnConfirm, btnCancle);
		borderPane.setCenter(pane);
	}

	/*
	 * 设置
	 */
	private void showSetting(BorderPane borderPane) {
		if (borderPane.getCenter() != null) {
			borderPane.setCenter(null);
			return;
		}
		getParams();
		Pane pane = new Pane();
		pane.setPrefSize(500, 200);
		pane.setStyle("-fx-background-color:#000");
		TabPane tabPane = new TabPane();
		tabPane.setPrefSize(500, 178);
		tabPane.setSide(Side.LEFT);
		tabPane.setStyle("-fx-background-color:#000");
		tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);

		final Tab tab1 = new Tab();
		tab1.setText("基础设置");
		tab1.setStyle("-fx-font-size:16px");
		Pane panebasis = new Pane();
		panebasis.setPrefSize(480, 178);
		Label lblSavePath = new Label("视频存储位置：");
		lblSavePath.setTextFill(Color.WHITE);
		lblSavePath.setFont(Font.font(16));
		lblSavePath.setLayoutX(10);
		lblSavePath.setLayoutY(75);
		TextField txtPath = new TextField();
		txtPath.setFont(Font.font(16));
		txtPath.setPrefSize(220, 35);
		txtPath.setLayoutX(120);
		txtPath.setLayoutY(65);
		txtPath.setText(Constant.SAVE_PATH);

		Button btnSet = new Button("设置");
		btnSet.setFont(Font.font(16));
		btnSet.setTextFill(Color.WHITE);
		btnSet.setStyle("-fx-background-color:#808a87");
		btnSet.setLayoutX(350);
		btnSet.setLayoutY(65);
		btnSet.setOnAction(e -> {
			DirectoryChooser directoryChooser = new DirectoryChooser();
			File file = directoryChooser.showDialog(this);
			if (file != null) {
				txtPath.setText(file.getPath().toString());
			}

		});

		Button btnView = new Button("浏览");
		btnView.setFont(Font.font(16));
		btnView.setTextFill(Color.WHITE);
		btnView.setStyle("-fx-background-color:#808a87");
		btnView.setLayoutX(410);
		btnView.setLayoutY(65);
		btnView.setOnAction(e -> {
			try {
				Desktop.getDesktop().open(new File(txtPath.getText().toString()));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		});

		panebasis.getChildren().addAll(lblSavePath, txtPath, btnSet, btnView);

		tab1.setContent(panebasis);

		final Tab tab2 = new Tab();
		tab2.setText("直播设置");
		tab2.setStyle("-fx-font-size:16px");
		Pane paneLive = new Pane();
		paneLive.setPrefSize(480, 178);
		Label lblLive = new Label("直播地址：");
		lblLive.setTextFill(Color.WHITE);
		lblLive.setFont(Font.font(18));
		lblLive.setLayoutX(40);
		lblLive.setLayoutY(75);
		TextField txtLive = new TextField();
		txtLive.setFont(Font.font(16));
		txtLive.setPrefSize(280, 35);
		txtLive.setLayoutX(130);
		txtLive.setLayoutY(65);
		paneLive.getChildren().addAll(lblLive, txtLive);
		tab2.setContent(paneLive);
		tabPane.getTabs().addAll(tab1, tab2);
		tabPane.setStyle("-fx-background-color:#000");

		Button btnConfirm = new Button("确定");
		btnConfirm.setFont(Font.font(16));
		btnConfirm.setTextFill(Color.WHITE);
		btnConfirm.setStyle("-fx-background-color:#808a87");
		btnConfirm.setLayoutX(320);
		btnConfirm.setLayoutY(178);
		btnConfirm.setOnAction(e -> {
			Constant.SAVE_PATH = txtPath.getText().toString();
			Constant.LIVE_PATH = txtLive.getText().toString();
			Map<String, String> map = new HashMap<>();
			map.put("SAVE_PATH", txtPath.getText().toString()); // 激活标致
			PropertiesUtil.setValue(System.getProperty("user.dir") + File.separatorChar + "config/savepath.properties",
					map);
			borderPane.setCenter(null);
			if (isLive) {
				localScreentRecordAndPush.screenRecord(txtLive.getText().toString());
			}
		});

		Button btnCancle = new Button("取消");
		btnCancle.setFont(Font.font(16));
		btnCancle.setTextFill(Color.WHITE);
		btnCancle.setStyle("-fx-background-color:#808a87");
		btnCancle.setLayoutX(400);
		btnCancle.setLayoutY(178);
		btnCancle.setOnAction(e -> {
			borderPane.setCenter(null);
		});
		pane.getChildren().addAll(tabPane, btnConfirm, btnCancle);
		borderPane.setCenter(pane);
	}

	private void getParams() {
		Properties properties = new Properties();

		// 使用InPutStream流读取properties文件，获取服务器的IP和端口号
		try {
			BufferedReader bufferedReader = new BufferedReader(
					new FileReader(System.getProperty("user.dir") + File.separatorChar + "config/savepath.properties"));
			properties.load(bufferedReader);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 获取key对应的value值
		String savePath = properties.getProperty("SAVE_PATH").toString();
		if (!"".equals(savePath)) {
			Constant.SAVE_PATH = savePath;
		}

	}

	/**
	 * 判断是否有录音设备
	 * 
	 * @return true or false
	 */
	private Boolean isHaveDevice() {
		Boolean result = false;
		AudioFormat audioFormat = new AudioFormat(44100.0F, 16, 2, true, false);
		DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
		try {
			TargetDataLine line = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
			result = true;
			line.stop();
			line.close();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			result = false;
		}
		return result;
	}

	/**
	 * 计时器
	 * 
	 * @author gj
	 *
	 */
	private class CountingThread extends Thread {

		volatile public boolean stopped = true; // 停止标志

		private CountingThread() {
			setDaemon(true);
		}

		@Override
		public synchronized void run() {
			while (true) {
				if (!stopped) {
					long elapsed = System.currentTimeMillis() - programStart - pauseCount; // 当前录制时间
					Platform.runLater(() -> {
						lblTime.setText(format(elapsed));
					});
				}

				try {
					sleep(1000); // 1秒更新一次显示
				} catch (InterruptedException e) {
					e.printStackTrace();
					break;
				}
			}
		}

		// 将毫秒数格式化
		private String format(long elapsed) {
			int hour, minute, second;

			elapsed = elapsed / 1000;

			second = (int) (elapsed % 60);
			elapsed = elapsed / 60;

			minute = (int) (elapsed % 60);
			elapsed = elapsed / 60;

			hour = (int) (elapsed % 60);

			return String.format("%02d:%02d:%02d", hour, minute, second);
		}
	}

}
